package cn.edu.nju.contact.contacts;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.edu.nju.contact.R;
import cn.edu.nju.contact.add.ContactAddActivity;
import cn.edu.nju.contact.group.GroupMainUIActivity;
import cn.edu.nju.contact.view.ContactViewActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ContactsUIActivity extends Activity implements OnGestureListener{
	private static final int ADD_CONTACT_REQUEST = 0;
	private static final int EDIT_CONTACT_REQUEST = 1;
	private static final int VIEW_CONTACT_REQUEST = 2;
	
	TextView textView_title;
	
	Button contact_add_button;
	Button contact_group_button;
	
	ListView contactListView;
	ContactItemContainer<ContactItem> contactsContainer;
	
	ArrayList<ContactItem> recommendContact;
	
	GestureDetector mGestureDetector;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contacts);
		
		textView_title = (TextView)findViewById(R.id.textView_contact_title);
		
		contact_add_button = (Button)findViewById(R.id.button_add_contact);
		contact_add_button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent(ContactsUIActivity.this, ContactAddActivity.class);
				startActivityForResult(intent, ADD_CONTACT_REQUEST);
			}
		});
		
		
		contactListView = (ListView)findViewById(R.id.listView_contact);	
		loadContactListData();
		Map<String, ArrayList<ContactItem>> sortedContacts = contactsContainer.getSortedData();
		
		recommendContact = new ArrayList<ContactItem>();
		ContactItem r_item = new ContactItem((String)getText(R.string.recommend), "123-456-6", null);
		recommendContact.add(r_item);
		

		ContactItem item_me = new ContactItem("Me", "888-888-88", null);
		ArrayList<ContactItem> array_me = new ArrayList<ContactItem>();
		array_me.add(item_me);
		sortedContacts.put((String)getText(R.string.myself), array_me);
		sortedContacts.put((String)getText(R.string.recommend), recommendContact);
		
		SeparatedListAdapter adapter = makeAdapter(sortedContacts);
		contactListView.setAdapter(adapter);
		
		mGestureDetector = new GestureDetector(this);
		contactListView.setOnTouchListener(new OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				return mGestureDetector.onTouchEvent(event);
			}
		});
		contactListView.setLongClickable(true);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	// TODO just for prototype
	private void loadContactListData(){
		contactsContainer = new ContactItemContainer<ContactItem>();
		String[] names = {"Cormack", "Dewey", "Cray", "Babbage", "Boole", "Berners-Lee", "Atanasoff ", "Allen", "Dijkstra ", "Dix", "Erdos"};
		for(int i = 0; i < 11; i++){
			ContactItem item = new ContactItem(names[i], "123-456-" + i, null);
			contactsContainer.addData(item);
		}
		textView_title.setText(getText(R.string.contact) + "(" + 11 +")");
	}
	
	private <T extends ContactItemInterface<T>> SeparatedListAdapter makeAdapter(Map<String, ArrayList<T>> sortedObjects)
    {
    	Set<String> keys = sortedObjects.keySet();
    	List<String> sortedKeys = new ArrayList<String>(keys);
    	Collections.sort(sortedKeys);
        Iterator<String> it = sortedKeys.iterator();
        SeparatedListAdapter adapter = new SeparatedListAdapter(this);
        String label = null;
        ArrayList<T> section = new ArrayList<T>();
        while (it.hasNext()) {
        	label = it.next();
        	section = sortedObjects.get(label);
        	// TODO this cast is ugly
            adapter.addSection(label, new ContactListAdapter(this, (List<ContactItem>)section));
        }
        return adapter;
    }

	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if(e2.getX() - e1.getX() > 50){
			Intent intent = new Intent(ContactsUIActivity.this, GroupMainUIActivity.class);
			startActivity(intent);
		}
		return false;
	}

	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
}
